<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class AdminUserModel extends Model
{

    // 设置数据表（不含前缀）
    protected $name = 'adminuser';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ld_adminuser';

    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 查询用户名对应密码戳
     */
    public function findPw($where)
    {
        $result = Db::name($this->name)->where($where)->field("id,password,authgroup,disable,special,isdelete,position,manual,Versiontype")->find();
        return $result;
    }


    /**
     * @param $uid
     * @param $data
     * @param 修改登录信息
     */
    public function Upuserlist($uid,$data){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where('id',$uid)->update($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $data
     * @param 添加用户
     */
    public function adduser($data){
        $query = Db::name($this->name)->insertGetId($data);
        return $query;
    }


    /**
     * @param $id
     * @param $data
     * @param 账户 启用/禁用
     */
    public function examineTs($id,$data){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where('id',$id)->update($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $uid
     * @param $data
     * @param 修改用户
     */
    public function edit($uid,$data){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where('id',$uid)->update($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $where
     * @param $data
     * @param 删除用户
     */
    public function dele($where=[],$data=[]){
        // 启动事务
        Db::startTrans();
        try{
            $query = Db::name($this->name)->where($where)->update($data);
            // 提交事务
            Db::commit();
            return $query;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * @param $where
     * @param $data
     * @param 分配类型
     */
    public function versio($where,$data){
        $query = Db::name($this->name)->where($where)->update($data);
        return $query;
    }


    /**
     * @param $where
     * @param 获取当前分类
     */
    public function Verasc($where,$ziduan){
        $query = Db::name($this->name)->where($where)->field($ziduan)->find();
        return $query;
    }
    

    /**
     * @param $where
     * @param 获取用户列表信息
     */
    public function userlist($where=[],$page,$limit)
    {
        $query = Db::name($this->name)
            ->alias('a')
            ->where($where)
            ->join('auth_group_access b', 'a.id=b.uid','left')
            ->join('auth_group c', 'c.id=b.group_id','left')
            ->order('a.createTime desc')
            ->field('a.id,a.username,a.disable,a.loginTime,a.createTime,a.position,a.loginIp,c.title,a.manual,a.Versiontype,a.name,a.phone')
            ->page($page)
            ->limit($limit)
            ->select();
        return $query;
    }

    public function county($where)
    {
        $query = Db::name($this->name)->where($where)->count();
        return $query;
    }
    
}